<template>
  <div class="app-container">
    <!-- 搜索区域优化 -->
    <div class="search-container">
      <el-form 
        :model="queryParams" 
        ref="queryForm" 
        size="small" 
        :inline="true" 
        v-show="showSearch" 
        label-width="68px"
        class="search-form"
      >
        <el-form-item label="用户ID" prop="customerId">
          <el-input
            v-model="queryParams.customerId"
            placeholder="请输入用户ID"
            clearable
            @keyup.enter.native="handleQuery"
            class="search-input"
          />
        </el-form-item>
        <el-form-item label="用户名字" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入用户名字"
            clearable
            @keyup.enter.native="handleQuery"
            class="search-input"
          />
        </el-form-item>
        <el-form-item label="封禁时间" prop="time">
          <el-date-picker 
            clearable
            v-model="queryParams.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择封禁时间"
            class="search-datepicker"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            icon="el-icon-search" 
            size="mini" 
            @click="handleQuery"
            class="search-btn"
          >搜索</el-button>
          <el-button 
            icon="el-icon-refresh" 
            size="mini" 
            @click="resetQuery"
            class="reset-btn"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作按钮区域优化 -->
    <el-row :gutter="10" class="operation-bar mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:ban:edit']"
          class="operation-btn"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:ban:export']"
          class="operation-btn"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格区域优化 -->
    <div class="table-container">
      <el-table 
        v-loading="loading" 
        :data="banList" 
        @selection-change="handleSelectionChange"
        border
        stripe
        class="main-table"
        :cell-style="{ padding: '10px 0' }"
        :header-cell-style="{ 
          backgroundColor: '#f5f7fa', 
          color: '#333',
          fontWeight: '500',
          padding: '12px 0'
        }"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="黑名单ID" align="center" prop="id" />
        <el-table-column label="用户ID" align="center" prop="customerId" />
        <el-table-column label="用户名字" align="center" prop="name" />
        <el-table-column label="封禁原因" align="center" prop="reason" show-overflow-tooltip />
        <el-table-column label="封禁时间" align="center" prop="time" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="处罚内容" align="center" prop="content" show-overflow-tooltip />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:ban:edit']"
              class="table-operation-btn"
            >修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <!-- 分页区域优化 -->
    <div class="pagination-container">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        class="pagination-component"
      />
    </div>

    <!-- 弹窗样式优化 -->
    <el-dialog 
      :title="title" 
      :visible.sync="open" 
      width="600px" 
      append-to-body
      class="custom-dialog"
    >
      <el-form 
        ref="form" 
        :model="form" 
        :rules="rules" 
        label-width="80px"
        class="dialog-form"
      >
        <el-form-item label="用户ID" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入用户ID" class="dialog-input" />
        </el-form-item>
        <el-form-item label="用户名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户名字" class="dialog-input" />
        </el-form-item>
        <el-form-item label="封禁原因" prop="reason">
          <el-input 
            v-model="form.reason" 
            type="textarea" 
            placeholder="请输入内容" 
            class="dialog-textarea"
            rows="3"
          />
        </el-form-item>
        <el-form-item label="封禁时间" prop="time">
          <el-date-picker 
            clearable
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择封禁时间"
            class="dialog-datepicker"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处罚内容">
          <editor v-model="form.content" :min-height="192" class="dialog-editor" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" class="dialog-btn cancel-btn">取 消</el-button>
        <el-button type="primary" @click="submitForm" class="dialog-btn confirm-btn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBan, getBan, updateBan } from "@/api/system/ban"  // 移除了addBan和delBan

export default {
  name: "Ban",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 封禁原因表格数据
      banList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        name: null,
        reason: null,
        time: null,
        content: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询封禁原因列表 */
    getList() {
      this.loading = true
      listBan(this.queryParams).then(response => {
        this.banList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        name: null,
        reason: null,
        time: null,
        content: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getBan(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改封禁原因"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 只保留修改逻辑
          updateBan(this.form).then(response => {
            this.$modal.msgSuccess("修改成功")
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/ban/export', {
        ...this.queryParams
      }, `ban_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped>
/* 搜索区域样式 */
.search-container {
  padding: 12px 15px;
  background-color: #f9fafc;
  border-radius: 4px;
  margin-bottom: 15px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.search-form {
  margin: 0;
}

.search-input, .search-datepicker {
  width: 200px !important;
  margin-right: 8px;
}

.search-btn {
  margin-right: 8px;
}

/* 操作按钮区域样式 */
.operation-bar {
  padding: 8px 15px;
  background-color: #fff;
  border-radius: 4px;
  margin-bottom: 15px !important;
  border: 1px solid #eaeaea;
}

.operation-btn {
  transition: all 0.2s ease;
}

.operation-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 表格区域样式 */
.table-container {
  background-color: #fff;
  border-radius: 4px;
  padding: 10px 15px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.main-table {
  width: 100%;
}

.table-operation-btn {
  margin: 0 5px;
  transition: color 0.2s ease;
}

.table-delete-btn {
  color: #f56c6c;
}

.table-delete-btn:hover {
  color: #e4393c;
}

/* 分页区域样式 */
.pagination-container {
  margin-top: 15px;
  text-align: right;
  padding-right: 15px;
}

/* 弹窗样式 */
.custom-dialog {
  border-radius: 6px;
  overflow: hidden;
}

.dialog-form {
  margin-top: 15px;
}

.dialog-input, .dialog-datepicker {
  width: 100%;
}

.dialog-textarea {
  width: 100%;
  resize: vertical;
}

.dialog-editor {
  width: 100%;
}

.dialog-footer {
  padding: 10px 20px 15px;
  border-top: 1px solid #f0f0f0;
  margin-top: 15px;
}

.dialog-btn {
  min-width: 80px;
  margin-left: 10px;
}

.cancel-btn {
  margin-left: 0;
}

/* 响应式调整 */
@media screen and (max-width: 1200px) {
  .search-input, .search-datepicker {
    width: 160px !important;
  }
}

@media screen and (max-width: 992px) {
  .search-input, .search-datepicker {
    width: 140px !important;
  }
}
</style>